/*
 * Copyright 2013 Glassy Cinema.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package hu.glassy.web.page.movie.edit;

import hu.glassy.core.model.Movie;
import hu.glassy.web.model.MovieModel;
import hu.glassy.web.panel.movie.edit.MovieEditPanel;
import org.apache.wicket.RestartResponseAtInterceptPageException;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;

/**
 * Page which allows to create or edit movie data
 *
 * @author Alexander Molnár <molnar.alexander@sonrisa.hu>
 */
public class MovieEditPage extends MovieAddPage
{

  public MovieEditPage(final PageParameters parameters)
  {
    super(parameters);
  }

  @Override
  public IModel<String> getPageTitleModel()
  {
    return new ResourceModel("movie.edit.page.title");
  }

  @Override
  public Panel getContentPanel(final String panelId, final PageParameters parameters)
  {
    final Long movieId = parameters.get("id").toLong();
    final IModel<Movie> movieModel = new MovieModel(movieId);
    // If the movie dows not exist in the database, redirect ot the add page...
    if (movieModel.getObject() == null)
    {
      throw new RestartResponseAtInterceptPageException(MovieAddPage.class);
    }

    return new MovieEditPanel(panelId, movieModel);
  }
}
